package com.heyqing.topic.HasPathSum_0112;

import java.util.Objects;

/**
 * ClassName:HasPathSum
 * Package:com.heyqing.topic.HasPathSum_0112
 * Description:
 *
 * @Date:2024/9/14
 * @Author:Heyqing
 */
public class HasPathSum {

    private boolean res = false;
    private boolean flag = false;

    public boolean hasPathSum(TreeNode root, int targetSum) {
        achieveFunc(root, 0, targetSum);
        return res;
    }

    private void achieveFunc(TreeNode root, int currentSum, int targetSum) {
        if (flag) return;
        if (Objects.isNull(root)) return;
        currentSum += root.val;
        if (currentSum == targetSum && Objects.isNull(root.left) && Objects.isNull(root.right)) {
            res = true;
            flag = true;
            return;
        }
        achieveFunc(root.left, currentSum, targetSum);
        achieveFunc(root.right, currentSum, targetSum);
    }
}
